package pt.iscte.pidesco.app.internal;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

import pt.iscte.pidesco.app.internal.PidescoAppActivator.ViewComponent;
import pt.iscte.pidesco.extensibility.IscdeComponent;

public class GenericPidescoView extends ViewPart {

//	public static final String ID = "pa.iscde.core.view";

	private static final String IMAGES_FOLDER = "images/";

//	private String pluginId;
//	private String viewId;
	private ViewComponent vcomponent;
//	private IscdeComponent component;
//	private String title;

	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);
		
		vcomponent = PidescoAppActivator.getInstance().getComponent(site.getSecondaryId());
//		component = vc.component;
//		pluginId = vc.pluginId;
//		title = vc.viewTitle;
		IActionBars bars = getViewSite().getActionBars();
		fillLocalToolBar(bars.getToolBarManager());
	}

	@Override
	public void createPartControl(Composite parent) {
		setPartName(vcomponent.viewTitle);
		setTitleImage(vcomponent.icon);
		setTitleToolTip("Contributor: " + vcomponent.pluginId);
		parent.setLayout(new FillLayout());
		Map<String, Image> imagesMap = buildImageMap();
		vcomponent.createContents(parent, imagesMap);
	}

	private Map<String, Image> buildImageMap() {
		Map<String, Image> imagesMap = new HashMap<String, Image>();
		Bundle bundle = Platform.getBundle(vcomponent.pluginId);
		Enumeration<String> imgs = bundle.getEntryPaths(IMAGES_FOLDER);
		if(imgs != null) {
			while(imgs.hasMoreElements()) {
				String path = imgs.nextElement();
				String key = path.substring(IMAGES_FOLDER.length());
				try {
					ImageDescriptor desc = PidescoAppActivator.imageDescriptorFromPlugin(vcomponent.pluginId, path);
					if(desc != null) {
						imagesMap.put(key, desc.createImage());
					}
				}
				catch(SWTException e) {

				}
			}
		}
		return imagesMap;
	}



	private void fillLocalToolBar(IToolBarManager manager) {

		Action action1 = new Action("ACT",IAction.AS_PUSH_BUTTON) {
			public void run() {
				System.out.println("??");
			}
		};
		action1.setText("Action 1");
		action1.setToolTipText("Action 1 tooltip");
		action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
				getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
		manager.add(action1);
		manager.add(new Separator());
		Action action2 = new Action("ACT2",IAction.AS_CHECK_BOX) {
			public void run() {
				System.out.println("??");
			}
		};
		action2.setText("Action 2");
		action2.setToolTipText("Action 2 tooltip");
		action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
				getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
		manager.add(action2);
	}

	@Override
	public String getTitleToolTip() {
		return super.getTitleToolTip();
	}



	@Override
	public void setFocus() {

	}



//	public String getPluginId() {
//		return pluginId;
//	}

	//	public String getViewId() {
	//		return viewId;
	//	}
}
